<template>
  <BasicForm @register="registerValiate" class="my-5" />
</template>
<script lang="ts" setup>
  import { BasicForm, FormSchema, useForm } from '@/components/Form';
  // import { useMessage } from '@/hooks/web/useMessage';
  // src\api\escloud\upload.ts
  import { uploadApi } from '@/api/escloud/upload';
  import { watch } from 'vue';

  // const { createMessage } = useMessage();
  // 获取外部传入的数据
  const props = defineProps({
    fileList: {
      type: Object,
      default: () => {
        return {};
      },
    },
    batchId: {
      type: String,
      default: () => {
        return '';
      },
    },
    isView: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
  });
  console.log('uploadApi', uploadApi);

  const schemasValiate: FormSchema[] = [
    {
      field: 'fileList',
      component: 'ImageUpload',
      label: '',
      colProps: {
        span: 24,
      },
      componentProps: {
        multiple: true,
        accept: ['png', 'jpeg', 'jpg', 'pdf', 'xlsx', 'xls', 'doc', 'docx'],
        maxNumber: 50,
        maxSize: 50,
        api: uploadApi,
        disabled: props.isView,
        uploadParams: {
          configCode: 'eslogis_order',
          label: 'contract_template',
          batchId: props.batchId,
        },
      },
    },
  ];

  const [registerValiate, { setFieldsValue, updateSchema }] = useForm({
    labelWidth: 160,
    schemas: schemasValiate,
    actionColOptions: {
      span: 18,
    },
    showSubmitButton: false,
    showResetButton: false,
  });

  watch(
    () => props.fileList,
    (val) => {
      console.log('watch-fileList', val);
      const temp = val.map((item) => {
        return {
          ...item,
          uid: item.id,
          name: item.fileName,
          url: item.fileUrl,
          status: 'done',
        };
      });
      // console.log('watch-temp', temp);
      setFieldsValue({ fileList: temp });
    },
  );

  watch(
    () => props.batchId,
    (val) => {
      console.log('监听batchId=====================', val);
      updateSchema({
        field: 'fileList',
        componentProps: { uploadParams: { batchId: val } },
      });
    },
  );

  console.log('info', props);
</script>

<style scoped></style>
